<extend name="./Public/base" />
<block name="title">登陆</block>
<block name="head">
    <load href="__PUBLIC__/Css/login.css"/>
</block>
<block name="main">

    <div class="alert alert-danger hidden" role="alert"></div>
    <div class="inner-bg">
        <div class="container">
            <!-- <div class="row">
                <div class="col-sm-8 col-sm-offset-2 text">
                    <h1><strong> 欢迎访问, 请先登录</strong></h1>
                </div>
            </div> -->
            <div class="row">
                <div class="col-sm-4 col-sm-offset-4 form-box">
                    <div class="form-top">
                        <div class="form-top-left">
                            <h3 class="form-title" >教师登陆 </h3>
                        </div>
                        <div class="form-top-right">
                            <button class="btn btn-sm btn-primary" id="switch-ident"><span class="glyphicon glyphicon-chevron-left"></span>切换身份</button>
                        </div>
                    </div>
                    <div class="form-bottom">
                        <form role="form" action="" method="post" class="login-form">
                            <div class="form-group">
                                <label class="sr-only" for="form-username">用户名</label>
                                <input type="text" name="form-username" placeholder="请输入邮箱" class="form-username form-control" id="form-username" required>
                            </div>
                            <div class="form-group">
                                <label class="sr-only" for="form-password">密码</label>
                                <input type="password" name="form-password" placeholder="请输入密码" class="form-password form-control" id="form-password" required>
                            </div>
                            <!-- <div class="form-group">
                                <input type="text" name="form-verify" placeholder="请输入验证码" class="form-control form-verify" required>
                                <img id="verify-img" src="__MODULE__/Auth/getVerify" title="单击刷新" alt="单击刷新" onclick='this.src=this.src+"?r="+Math.random()'/>
                            </div> -->
                            <div class="checkbox">
                                <label>
                                    <input  type="checkbox" id="form-remember" checked> 记住我
                                </label>
                                <a class="pull-right" href="">忘记密码</a>
                            </div>
                            <button type="submit" class="btn">登陆</button>
                        </form>
                    </div>
                    <div class="form-bottom-message">
                        <p>没有账号? <a href="__MODULE__/Auth/register"> 点击注册</a></p>
                    </div>
                </div>
            </div>
            <!-- <div class="row">
                <div class="col-sm-6 col-sm-offset-3 switch-login">
                    <h4>切换身份登陆:</h4>
                    <div class="social-login-buttons">
                        <button class="btn btn-primary" style=""><li class="glyphicon glyphicon-user"></li> 学生登陆</button>
                    </div>
                </div>
            </div> -->
        </div>
    </div>

    <script type="text/javascript">
        $(function(){
            //获取一系列要用到的对象..
            var obj_number =  $("#form-username");
            var obj_password =  $("#form-password");
            var obj_verify =  $("#form-verify");
            var obj_remember =  $("#form-remember");
            var curr_state = "{$Think.cookie.isTeacher}"=="true"?true:false;
            var btn_switch = $("#switch-ident");
            var old_id = "{$Think.cookie.id}";
            var old_pwd = "{$Think.cookie.pwd}";
            var title = $(".form-title");
            var btn_switch = $("#switch-ident");
            var switchMess;
            //自动登入有错误
            (switchMess = function () {
                if (curr_state) {
                    obj_number.val(old_id);
                    obj_password.val(old_pwd);
                    title.html("教师登陆");
                }
                else {
                    obj_number.val(old_id);
                    obj_password.val(old_pwd);
                    title.html("学生登陆");
                }
                obj_verify.val("");
            })();
            btn_switch.click(function() {
                $(this).blur();
                curr_state = !curr_state;
                switchMess();
            });
            //点击登陆事件处理
            $(".login-form").submit(function(e) {
                e.preventDefault();          //阻止表单的提交
                $("button[type=submit]").blur();             //单击后马上失焦,为了好看
                var number = obj_number.val();
                var password = obj_password.val();
                var verify = obj_verify.val();
                var remember = obj_remember.is(":checked");
                //var isTeacher = obj_isTeacher.is(":checked");
                if (number !== "" && password !== "" && verify !== "") {
                    //组织ajax,向后代发送请求.
                    //老师
                    if (curr_state) {
                        postData1 = {
                            number: number,
                            password: password,
                            verify: verify,
                            remember: remember,
                            isTeacher: curr_state
                        };
                        postApi("__MODULE__/Auth/check_login", postData1, function (err, data) {
                            if (err) {
                                showError(err.message);
                            }
                            else {
                                //所有验证都已经通过了.跳转到主页面
                                window.location.href = "__MODULE__/TeaClass/classes";
                            }
                        });
                    }
                    else {    //学生
                        postData2 = {
                            number: number,
                            password: password,
                            verify: verify,
                            remember: remember,
                            isTeacher: curr_state
                        };
                        postApi("__MODULE__/Auth/check_login", postData2, function (err, data) {
                            if (err) {
                                showError(err.message);
                            } else {
                                //所有验证都已经通过了.跳转到主页面
                                window.location.href = "__MODULE__/StuClass/classes";
                            }
                        });
                    }
                }
                else{
                    if(number==""){
                        showError("您的登录邮箱号不能为空");
                        obj_number.focus();
                        //obj_number.addClass("input-error");
                    }
                    else if(password==""){
                        showError("您的密码不能为空");
                        obj_password.addClass("input-error");
                        //obj_password.focus();
                    }
                    else{
                        showError("验证码不能为空");
                        obj_verify.focus();
                    }
                }
            });

        });
    </script>
</block>